$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'watersub/vaccountmanagesub/list',
        datatype: "json",
        colModel: [			
			{ label: '小区', name: 'proName', index: 'pro_name', width: 180, key: true },
            { label: '设备数', name: 'deviceCount', index: 'device_count', width: 80 },

            { label: '保洁表数', name: 'cleanCount', index: 'clean_count', width: 80 },
            { label: '网点表数', name: 'salesCount', index: 'sales_count', width: 80 },
            { label: '小区说明', name: 'proDesc', index: 'pro_desc', width: 200 },

            { label: '是否竣工', name: 'installerPhone', index: 'installer_phone', width: 80,
                formatter: function(value, options, row){
                    return value === '已竣工' ?
                        '<span class="label label-success">已竣工</span>' :
                        '<span class="label label-danger">未知</span>';
                } },
            { label: '调试人', name: 'debugerName', index: 'debuger_name', width: 80 },
            { label: '竣工时间', name: 'installerName', index: 'installer_name', width: 80 },
            { label: '分所', name: 'unitNameG', index: 'unit_name_g', width: 120,hidden:true },
			{ label: 'mapX', name: 'mapX', index: 'map_x', width: 80,hidden:true },
			{ label: 'mapY', name: 'mapY', index: 'map_y', width: 80,hidden:true },

			{ label: '采集方式', name: 'modeName', index: 'mode_name', width: 80 },
			{ label: '电话', name: 'phone', index: 'phone', width: 150,hidden:true },
			{ label: '备注', name: 'memo', index: 'memo', width: 80,hidden:true },
            { label: 'beforedata', name: 'beforedata', index: 'beforedata', width: 80,hidden:true },
			{ label: 'afterdata', name: 'afterdata', index: 'afterdata', width: 80,hidden:true },
			{ label: 'beforetotaldata', name: 'beforetotaldata', index: 'beforetotaldata', width: 80,hidden:true },
			{ label: 'aftertotaldata', name: 'aftertotaldata', index: 'aftertotaldata', width: 80,hidden:true },
			{ label: 'faultcount', name: 'faultcount', index: 'faultcount', width: 80,hidden:true },
			{ label: 'areaId', name: 'areaId', index: 'area_id', width: 80,hidden:true  },
			{ label: 'unitNameWId', name: 'unitNameWId', index: 'unit_name_w_id', width: 80,hidden:true  },
			{ label: 'cityId', name: 'cityId', index: 'city_id', width: 80,hidden:true  },
			{ label: 'unitNameGId', name: 'unitNameGId', index: 'unit_name_g_id', width: 80,hidden:true  },
			{ label: 'modeId', name: 'modeId', index: 'mode_id', width: 80,hidden:true  },
            { label: '管理所', name: 'unitNameW', index: 'unit_name_w', width: 80,hidden:true },
            { label: '分公司', name: 'areaName', index: 'area_name', width: 120,hidden:true },
            { label: '城市', name: 'cityName', index: 'city_name', width: 80 ,hidden:true},
			{ label: 'abnormal', name: 'abnormal', index: 'abnormal', width: 80,hidden:true }
        ],
		viewrecords: true,
        height: $(window).height()-165,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 60,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        },
        ondblClickRow: function(rowid, iRow, iCol, e){
            $("#jqGrid").jqGrid('resetSelection');
            //$("#jqGrid").jqGrid('setSelection',$("#selRow").val(),false);
            $("#jqGrid").jqGrid('setSelection',rowid);
            var rowData = $("#jqGrid").jqGrid("getRowData", rowid);

            var detailGridName = '#jqChildGrid';
            vm.showChild = true;

            var page = $(detailGridName).jqGrid('getGridParam','page');
            if (vm.newpage == true) page = 1;
            $(detailGridName).jqGrid('setGridParam',{
                postData:{'ckey': rowData.proName,'dept':rowData.proName},
                page:page
            }).trigger("reloadGrid");

            jQuery(detailGridName).jqGrid('setCaption',
                "【" + rowData.proName+"】 "
                //+"【" + rowData.unitNameG+"】 "
                +"【" + rowData.unitNameW+"】 "
                +"【" + rowData.areaName+"】 "
                +"【"+ rowData.deviceCount + "】 "
            ).trigger('reloadGrid');

            //iframe自适应
            $(window).on('resize', function() {
                var $content = $('.content');
                $content.height($(this).height() - 154);
                $content.find('iframe').each(function() {
                    $(this).height($content.height());
                });
            }).resize();
        }
    });

    $("#jqChildGrid").jqGrid({
        url: baseURL + 'watersub/tclientinfosub/list',
        postData:{ckey : '-1'},
        datatype: "json",
        colModel: [
            { label: '设备号', name: 'deviceId', index: 'device_id',  key: true },
            { label: '客户编号', name: 'clientId', index: 'client_id',  },
            { label: '客户名称', name: 'clientName', index: 'client_name',},
            { label: '性别', name: 'clientSex', index: 'client_sex', width: 80,hidden:true },
            { label: '年龄', name: 'clientAge', index: 'client_age', width: 80,hidden:true },
            { label: 'IMEI', name: 'clientIdCard', index: 'client_id_card' },
            { label: '楼号', name: 'buildNum', index: 'build_num' },
            { label: '单元', name: 'unitNum', index: 'unit_num' },
            { label: '门牌号码', name: 'houseNum', index: 'house_num' },
            { label: '小区名', name: 'memo1', index: 'memo1'},
            { label: '内部名', name: 'proName', index: 'pro_name', width: 80,hidden:true  },
            { label: '备注2', name: 'memo2', index: 'memo2', width: 80,hidden:true },
            { label: '备注3', name: 'memo3', index: 'memo3', width: 80,hidden:true },
            { label: '二维码', name: 'qrcode', index: 'qrCode', width: 80,hidden:true }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqChildGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

    $(function () {
        $('#inputClear').click(function () {
            $('input')[0].value = "";
            vm.q.value="";
        })
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            key: '',
            value:null,
            dept:null,
            ckey: '',
        },
        newpage : false,
        searchKeyDictInfo: {},
		showList: true,
        showChild: false,
		title: null,
		vaccountmanage: {}
	},
	methods: {
        getSearchKeyDict: function(){
            $.get(baseURL + "sys/dict/list",{name:"qkey_accountmanage"}, function(r){
                vm.searchKeyDictInfo = r.page.list;
            });
        },
        query: function () {
            if(vm.q.value != null && vm.q.value != ""
                && (vm.q.key == null || vm.q.key == "")){
                alert('请选择查询条件!');
                return;
            }
            vm.newpage=true;

            vm.reload();
        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.vaccountmanage = {};
		},
		update: function (event) {
			var proName = getSelectedRow();
			if(proName == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(proName)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.vaccountmanage.proName == null ? "watersub/vaccountmanagesub/save" : "watersub/vaccountmanagesub/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.vaccountmanage),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var proNames = getSelectedRows();
			if(proNames == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "watersub/vaccountmanagesub/delete",
                        contentType: "application/json",
                        data: JSON.stringify(proNames),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(proName){
			$.get(baseURL + "watersub/vaccountmanagesub/info/"+proName, function(r){
                vm.vaccountmanage = r.vaccountmanage;
            });
		},
        outxls: function(){
            var total = $("#jqGrid").jqGrid('getGridParam', 'records') + "";
            var dept = vm.q.dept==null ? "" : vm.q.dept+  "";
            var url = baseURL
                + "watersub/vaccountmanagesub/export?params="
                + encodeURIComponent(JSON.stringify({key:vm.q.key,value:vm.q.value,dept:dept,limit:total,page:"1"}));

            //console.log(decodeURIComponent(url));
            location.href = url;
        },
		reload: function (event) {
            //console.log(vm.q.dept);

			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
            if(vm.newpage){page = "1";}
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'key': vm.q.key,'value':vm.q.value,'dept':vm.q.dept},
                page:page
            }).trigger("reloadGrid");
        }
    },
    created: function(){
        this.getSearchKeyDict();
    }
});